package com.elec.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.elec.domain.R;
import com.elec.mapper.NotificationRecordMapper;
import com.elec.domain.entity.NotificationRecord;
import com.elec.service.NotificationRecordService;
import com.elec.util.ThreadLocalUtil;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 记录系统向用户发送的所有通知(NotificationRecord)表服务实现类
 *
 * @author yu
 * @since 2024-07-05 13:42:59
 */
@Service("notificationRecordService")
public class NotificationRecordServiceImpl extends ServiceImpl<NotificationRecordMapper, NotificationRecord> implements NotificationRecordService {

    @Override
    public R add(NotificationRecord notificationRecord) {
        if (save(notificationRecord)) {
            return R.success("添加成功");
        }
        return R.error("添加失败");
    }

    @Override
    public R delete(Long id) {
        if (removeById(id)) {
            return R.success("删除成功");
        }
        return R.error("删除失败");
    }

    @Override
    public R update(NotificationRecord notificationRecord) {
        if (updateById(notificationRecord)) {
            return R.success("修改成功");
        }
        return R.error("修改失败");
    }

    @Override
    public R get(Long id) {
        if (getById(id) != null) {
            return R.success(getById(id));
        }
        return R.error("查询失败");
    }

    @Override
    public R getList(Integer pageNum, Integer pageSize, NotificationRecord notificationRecord) {
        LambdaQueryWrapper<NotificationRecord> queryWrapper = new LambdaQueryWrapper<>();
        if (notificationRecord != null) {
            if (notificationRecord.getNotificationId() != null) {
                queryWrapper.eq(NotificationRecord::getNotificationId, notificationRecord.getNotificationId());
            }
            if (notificationRecord.getSentDate() != null) {
                queryWrapper.eq(NotificationRecord::getSentDate, notificationRecord.getSentDate());
            }
            if (notificationRecord.getStatus() != null) {
                queryWrapper.eq(NotificationRecord::getStatus, notificationRecord.getStatus());
            }
            if (notificationRecord.getUserId() != null) {
                queryWrapper.eq(NotificationRecord::getUserId, notificationRecord.getUserId());
            }
        }
        Page<NotificationRecord> page = new Page<>(pageNum, pageSize);
        page(page, queryWrapper);
        return R.success(page.getRecords(), page.getTotal());
    }

    @Override
    public R getNotification() {
        Long userId = ThreadLocalUtil.getLocal();
        if (userId == null){
            userId = 1L;
        }
        LambdaQueryWrapper<NotificationRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(NotificationRecord::getUserId, userId);
        queryWrapper.eq(NotificationRecord::getStatus, "unread");
        List<NotificationRecord> list = list(queryWrapper);
        return R.success(list);
    }

    @Override
    public R getMyNotification() {
        Long userId = ThreadLocalUtil.getLocal();
        LambdaQueryWrapper<NotificationRecord> notificationWrapper = new LambdaQueryWrapper<>();
        notificationWrapper.eq(NotificationRecord::getUserId, userId);
        notificationWrapper.eq(NotificationRecord::getStatus, "unread");
        List<NotificationRecord> list = list(notificationWrapper);
        return R.success(list);
    }

    @Override
    public R readNotification(Long id) {
        if (getById(id) != null) {
            NotificationRecord notificationRecord = getById(id);
            notificationRecord.setStatus("read");
            updateById(notificationRecord);
            return R.success("已读");
        }
        return R.error("阅读失败");
    }
}


